﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToracTechnologies.ExpressionTreeLibrary.DynamicApi;

namespace ToracTechnologies.ExpressionTreeLibrary.ExtensionMethods.IQueryable
{

    /// <summary>
    /// Provides Extension Methods For IQueryable
    /// </summary>
    public static class IQueryableExtMethods
    {

        #region Paginate Results

        /// <summary>
        /// Takes The IQueryable And Grabs The Current Page We Are On
        /// </summary>
        /// <typeparam name="T">Type Of Records That Are Returned</typeparam>
        /// <param name="QueryToModify">Query To Modify</param>
        /// <param name="CurrentPageNumber">What Page Number Are You Currently On</param>
        /// <param name="HowManyRecordsPerPage">How Many Records Per Page</param>
        /// <returns>IQueryable Of T</returns>
        public static IQueryable<T> PaginateResults<T>(this IQueryable<T> QueryToModify, int CurrentPageNumber, int HowManyRecordsPerPage)
        {
            return EntityFrameworkUtilities.PaginateResults(QueryToModify, CurrentPageNumber, HowManyRecordsPerPage);
        }

        #endregion

        #region Order By

        /// <summary>
        /// Modifies Queryable Of T And Orders It For Entity Framework.
        /// </summary>
        /// <typeparam name="T">Type Of The IQueryable</typeparam>
        /// <param name="QueryToModify">Query To Modifiy</param>
        /// <param name="PropertyNameToCheck">Property Name To Sort By</param>
        /// <returns>IOrderedQueryable Of T</returns>
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> QueryToModify, string PropertyNameToCheck)
        {
            return EntityFrameworkUtilities.OrderBy(QueryToModify, PropertyNameToCheck, true);
        }

        /// <summary>
        /// Modifies Queryable Of T And Orders It For Entity Framework.
        /// </summary>
        /// <typeparam name="T">Type Of The IQueryable</typeparam>
        /// <param name="QueryToModify">Query To Modifiy</param>
        /// <param name="PropertyNameToCheck">Property Name To Sort By</param>
        /// <returns>IOrderedQueryable Of T</returns>
        public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> QueryToModify, string PropertyNameToCheck)
        {
            return EntityFrameworkUtilities.OrderBy(QueryToModify, PropertyNameToCheck, false);
        }

        #endregion

    }

}
